
<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

<!--TODO : Improve!!!-->

	<!--Missing authentification-->
    <p:processor name="oxf:request">
        <p:input name="config">
            <config >
                <include>/request/parameters</include>
                <include>/request/headers</include>
                <include>/request/method</include>
            </config>
        </p:input>
        <p:output name="data" id="request"/>
    </p:processor>

    <p:processor name="oxf:xslt">
        <p:input name="data" href="#request#xpointer(/request/headers)"/>
        <p:output name="data" id="headers"/>
        <p:input name="config">
            <xsl:stylesheet version="2.0">
                <xsl:template match="/">
                    <headers>
                        <xsl:apply-templates/>
                    </headers>
                </xsl:template>
                <xsl:template match="header">
                    <xsl:element name="{./name}">
                        <xsl:value-of select="value"/>
                    </xsl:element>
                </xsl:template>
            </xsl:stylesheet>
        </p:input>
    </p:processor>
<!-- TODO: remove parameters once HTTP POST available -->
    <p:processor name="oxf:xslt">
        <p:input name="data" href="#request"/>
        <p:output name="data" id="url"/>
        <p:input name="config">
            <config xsl:version="2.0">
                <url>
                    <xsl:value-of select="/request/headers/header[name='x-forward-url']/value"/>
                    <xsl:if test="not(contains(/request/headers/header[name='x-forward-url']/value, '?'))">?</xsl:if>
                    <xsl:if test="contains(/request/headers/header[name='x-forward-url']/value, '?')"><![CDATA[&]]></xsl:if>
                    <xsl:for-each select="/request/parameters/parameter[name != 'mwwrand']">
                                
                        <xsl:value-of select="encode-for-uri(name)"/>
                        <xsl:text>=</xsl:text>
                        <xsl:value-of select="encode-for-uri(value)"/>
                        <xsl:if test="position() != last()"><![CDATA[&]]></xsl:if>
                    </xsl:for-each>
                </url>
                <content-type>application/xml</content-type>
                <mode>xml</mode>
            </config>
         
        </p:input>
    </p:processor>

    <p:choose href="#request">
        <p:when test="/request/method = 'GET' or /request/method = 'POST'">
            <p:processor name="oxf:url-generator">
                <p:input name="config" href="#url"/>
                <p:output name="data" id="request-doc"/>
            </p:processor>
        </p:when>
        <p:otherwise>
            <!-- Should be possible to post/put to REST API using oxf:xforms-submission -->
            <p:processor name="oxf:xslt">
                <p:input name="data" href="#url"/>
                <p:output name="data" id="request-doc"/>
                <p:input name="config">
                    <error xsl:version="2.0" >Not implemented</error>
                </p:input>
            </p:processor>
        </p:otherwise>
    </p:choose>
	
    <p:processor name="oxf:xml-converter">
        <p:input name="config">
            <config>
                <content-type>text/html</content-type>
                <encoding>utf-8</encoding>
            </config>
        </p:input>
        <p:input name="data" href="#request-doc"/>
        <p:output name="data" id="xml-doc"/>
    </p:processor>

    <p:processor name="oxf:http-serializer">
        <p:input name="config">
            <config>
                <content-type>text/xml</content-type>
            </config>
        </p:input>
        <p:input name="data" href="#xml-doc"/>
    </p:processor>
</p:config>